#include "BorderLabel.h"
#include <QHBoxLayout>
#include <QFrame>
#include <QLabel>
#include <QString>
#include <QDebug>
CBorderLabel::CBorderLabel(bool bLeft,QWidget *parent) : QWidget(parent),_bLeft(bLeft)
{
    m_bIsPressed = false;
    setMouseTracking(true);
    setCursor(Qt::PointingHandCursor);

    _bgiFrame = new QFrame(this);
    _textFrame = new QFrame(this);

    _framelayout = new QHBoxLayout(_bgiFrame);
    _framelayout->setAlignment(Qt::AlignCenter|Qt::AlignRight);
    _framelayout->setSpacing(0);
    _framelayout->setMargin(0);
    _textFrame->setLayout(_framelayout);

    QString str = "";
    _label = new QLabel(this);
    _label->setAlignment(Qt::AlignBottom|Qt::AlignRight);
    _label->setText(str);
    _label->setStyleSheet("font-size:10px;color:white;");
    _framelayout->addWidget(_label);
}

void CBorderLabel::setGeometry(int x,int y,int width,int height)
{
    QRect rc = QRect(x,y,width,height);
    setGeometry(rc);
}

void CBorderLabel::setGeometry(const QRect &rect)
{
    _bgiFrame->setGeometry(rect);
    _textFrame->setGeometry(_bgiFrame->geometry());
}

//top-right-bottom-left
void CBorderLabel::setBorder(int top,int right,int bottom,int left)
{
    QRect rc = QRect(top,right,bottom,left);
    setBorder(rc);
}

void CBorderLabel::setBorder(const QRect &rc)
{
    _borderRect = rc;
    updateLabelBorderUI();
}

void CBorderLabel::setBorderImage(const QString &path,int top,int right,int bottom,int left)
{
    QRect rc = QRect(top,right,bottom,left);
    setBorderImage(path,rc);
}

void CBorderLabel::setBorderImage(const QString &path,const QRect &rc)
{
    _borderBGI = path;
    _borderBGIClipRect = rc;
    updateLabelBorderUI();
}

void CBorderLabel::updateLabelBorderUI()
{
    QString formatStr = QString("border-width:%1 %2 %3 %4;border-image:url(%5) %6 %7 %8 %9;")
            .arg(_borderRect.x()).arg(_borderRect.y()).arg(_borderRect.width()).arg(_borderRect.height())
            .arg(_borderBGI)
            .arg(_borderBGIClipRect.x()).arg(_borderBGIClipRect.y()).arg(_borderBGIClipRect.width()).arg(_borderBGIClipRect.height());
    _bgiFrame->setStyleSheet(formatStr);
}

void CBorderLabel::setTextAlignment(Qt::Alignment align)
{
    if(align & Qt::AlignRight)
        _label->setContentsMargins(0,0,5,0);
    else if(align & Qt::AlignLeft){
        _label->setContentsMargins(5,0,0,0);
    }
    _label->setAlignment(align);
    _framelayout->setAlignment(align);
}

void CBorderLabel::setText(const QString& str)
{
    _text = str;
    _label->setText(str);

    QFontMetrics fm1(_label->font());
    QRect rec1 = fm1.boundingRect(_text);

    int minWidth = _borderBGIClipRect.y()+_borderBGIClipRect.height();

    if(_bLeft){
        int dstWidth = 0;
        if(rec1.width()<minWidth)
            dstWidth = minWidth;
        else
            dstWidth = rec1.width();
        dstWidth+= 15;

        _textFrame->setFixedWidth(dstWidth);
        _bgiFrame->setFixedWidth(dstWidth);
    }else
    {
        QRect oldRect = _bgiFrame->geometry();
        int right = oldRect.x()+oldRect.width();

        int dstWidth = 0;
        if(rec1.width()<minWidth)
            dstWidth = minWidth;
        else
            dstWidth = rec1.width();
        dstWidth+= 15;
        QRect newRect = QRect(right-dstWidth,oldRect.y(),dstWidth,oldRect.height());
        _textFrame->setGeometry(newRect);
        _bgiFrame->setGeometry(newRect);
    }
}

void CBorderLabel::setFontSize(int size)
{
    _fontSize = size;
    _label->setStyleSheet(QString("font-size:%1px;color:%2;font-weight:bold;").arg(_fontSize).arg(_fontColor));
}

void CBorderLabel::setFontColor(QString color)
{
    _fontColor = color;
    _label->setStyleSheet(QString("font-size:%1px;color:%2;font-weight:bold;").arg(_fontSize).arg(_fontColor));
}

void CBorderLabel::setMargins(int left,int top,int right,int bottm)
{
    _framelayout->setContentsMargins(left,top,right,bottm);
}
